package com.party.service.inform.impl;

import com.party.common.Page;
import com.party.mapper.SysNoticeMapper;
import com.party.model.SysNotice;
import com.party.model.SysNoticeExample;
import com.party.service.inform.InformService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class InformServiceImpl implements InformService {

    @Resource
    SysNoticeMapper sysNoticeMapper;

    @Override
    public int addNotice(SysNotice sysNotice) {
        int num = sysNoticeMapper.insertSelective(sysNotice);
        return num;
    }

    @Override
    public int addNotice(SysNotice sysNotice, String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String uids[] = ids.split(",");
            for (String uid : uids) {
                sysNotice.setUid(Integer.parseInt(uid));
                num += sysNoticeMapper.insertSelective(sysNotice);
            }
        }
       return num;
    }

    @Override
    public int deleteNoticeByPrimaryKey(Integer id) {
        int num = sysNoticeMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += sysNoticeMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public SysNotice getNoticeByPrimaryKey(Integer id) {
        SysNotice sysNotice = sysNoticeMapper.selectByPrimaryKey(id);
        return sysNotice;
    }

    @Override
    public int updateNoticeByPrimaryKey(SysNotice sysNotice) {
        int num = sysNoticeMapper.updateByPrimaryKeySelective(sysNotice);
        return num;
    }

    @Override
    public Page getNoticeList(SysNotice sysNotice, int pageSize, int pageNumber) {
        SysNoticeExample sysNoticeExample = new SysNoticeExample();
        /**
         * 可以添加查询条件
         */
        if(sysNotice != null)
        {
            SysNoticeExample.Criteria criteria = sysNoticeExample.createCriteria();
            if(sysNotice.getTitle() != null && !"".equals(sysNotice.getTitle()))
            {
                criteria.andTitleLike("%"+sysNotice.getTitle()+"%");
            }

            if(sysNotice.getTime() != null && !"".equals(sysNotice.getTime()))
            {
                criteria.andTimeGreaterThan(sysNotice.getTime());
            }

            if(sysNotice.getStarttime() != null && !"".equals(sysNotice.getStarttime()))
            {
                criteria.andTimeGreaterThan(sysNotice.getStarttime());
            }

            if(sysNotice.getEndtime() != null && !"".equals(sysNotice.getEndtime()))
            {
                criteria.andTimeGreaterThan(sysNotice.getEndtime());
            }
        }
        sysNoticeExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = sysNoticeMapper.countByExample(sysNoticeExample);
        //获取指定的页码数据
        sysNoticeExample.setBeginNum((pageNumber - 1) * pageSize);
        sysNoticeExample.setEndNum(pageSize);
        List<SysNotice> list = sysNoticeMapper.getListPage(sysNoticeExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public Page getSysNoticeByUser(SysNotice sysNotice, int pageSize, int pageNumber) {

        //获取总的记录数量
        int count = sysNoticeMapper.countBySysNotice(sysNotice);

        //获取指定的页码数据
        sysNotice.setBeginNum((pageNumber - 1) * pageSize);
        sysNotice.setEndNum(pageSize);

        List<SysNotice> list = sysNoticeMapper.getSysNoticeByUser(sysNotice);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
